﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Sita.Entities.Invoice>" %>

<%@ Import Namespace="Sita.Presentation.Resources.Invoice" %>
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderContent" runat="server">
    <link rel="stylesheet" href="../../Content/Styles/WOStyle.css" type="text/css" media="screen"
        title="default" />
    <script src="../../Scripts/Shared/jQuery/ui/i18n/jquery.ui.datePicker-es.js" type="text/javascript"></script>
    <script src="<%: Html.AuthenticateUrl("~/Scripts/Invoice/Validation.js") %>" type="text/javascript"></script>
    <script type="text/javascript">
        AlreadyExistsMessage = "<%: Index.AlreadyExists %>";
        ExceedesStockMessage = "<%: Index.ExceedesStock %>";
    </script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div style="padding: 40px;">
        <h1>
            Crear / Editar Facturas</h1>
        <div id="InvoiceContent">
            <% Html.EnableClientValidation(); %>
            <% using (Ajax.BeginForm("CreateOrUpdate", "Invoice",
           new AjaxOptions() { OnComplete = "submitCompleted", HttpMethod = "Post" }, new { id = "InvoiceForm" }))
               {%>
            <% var creationDate = "";
               if (Model.CreationDate != null)
               {
                   creationDate = Model.CreationDate.Day + "/" + Model.CreationDate.Month + "/" + Model.CreationDate.Year;
               }%>
            <%: Html.ValidationSummary(true)%>
            <center>
                <%: Html.HiddenFor(model => model.InvoiceID)%>
                <%: Html.HiddenFor(model => model.BusinessUnitID)%>
                <%: Html.HiddenFor(model=>model.CustomerID) %>
                <div class="form-modulo">
                    <div class="form-modulo-title">
                        Cliente
                    </div>
                    <div class="form-modulo-box">
                        <%if (Model.CustomerID == 0)
                          { %>
                        <input type="text" id="CompanyName" style="width: 100px; height: 20px;" value="<%:ViewData["CustomerName"]%>" />
                        <%}
                          else
                          { %>
                        <label>
                            <%:ViewData["CustomerName"]%></label>
                        <%} %>
                    </div>
                </div>
                <div class="form-modulo">
                    <div class="form-modulo-title">
                        Fecha
                    </div>
                    <div class="form-modulo-box">
                        <input type="text" id="_creationDate" readonly="readonly" onkeydown="preventBackspace();"
                            value="<%: creationDate %>" />
                        <%: Html.HiddenFor(model => model.CreationDate)%>
                    </div>
                </div>
                <%--INICIO REMITOS--%>
                <div class="form-modulo">
                    <div class="form-modulo-title">
                        Remitos
                    </div>
                    <div class="form-modulo-box">
                        <select id="DeliveryNoteList">
                            <%if (Model.CustomerID != null)
                              { %>
                            <option value="0">Seleccionar...</option>
                            <%} %>
                            <%if (ViewData["DeliveryNoteList"] != null)
                              {
                                  foreach (Sita.Entities.DeliveryNote dn in (List<Sita.Entities.DeliveryNote>)ViewData["DeliveryNoteList"])
                                  {%>
                            <option value="<%: dn.DeliveryNoteID %>">
                                <%: dn.DeliveryNoteID%></option>
                            <%}
                          }%>
                        </select>
                        <input type="button" id="AddDeliveryNote" value="Agregar remito" class = "btn btn-lg btn-primary" style="float:right" />
                    </div>
                </div>
                <div style="width: 100%; height: 500px; text-align: center; vertical-align: middle;">
                    <table style="width: 100%;">
                        <thead style="border-bottom: 1px solid black; border-top: 1px solid black; background: whitesmoke">
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    &nbsp;
                                </th>
                                <th style="width: 50px">
                                    <%: Index.Number %>
                                </th>
                                <th>
                                    <%: Index.TotalPrice %>
                                </th>
                            </tr>
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                        </thead>
                        <tbody id="DeliveryNoteTable">
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                            <%--AGREGAR REMITOS PREEXISTENTES--%>
                            <% var rowDeliveryNoteIndex = 0;
                               decimal DeliveryNoteTotalPrice = 0; //Precio total de la sumatoria de remitos
                               foreach (Sita.Entities.InvoiceDeliveryNote deliveryNote in Model.InvoiceDeliveryNotes)
                               {%>
                            <tr id="rowDeliveryNote_<%: rowDeliveryNoteIndex%>" class="detail">
                                <td>
                                    <img src="../../Content/Images/cross.png" onclick="DeleteRow2(<%: rowDeliveryNoteIndex%>, 'DeliveryNote', <%: deliveryNote.DeliveryNoteID %>)" />
                                </td>
                                <td>
                                    <input type="text" readonly="readonly" value="<%: deliveryNote.DeliveryNoteID %>"
                                        id="DeliveryNote_DeliveryNoteID_<%: rowDeliveryNoteIndex%>" />
                                </td>
                                <td>
                                    <input readonly="readonly" type="text" id="DeliveryNote_TotalPrice_<%: rowDeliveryNoteIndex%>"
                                        value="<%: deliveryNote.DeliveryNote.DeliveryNoteTotalPrice %>" />
                                </td>
                            </tr>
                            <%rowDeliveryNoteIndex++;
                              DeliveryNoteTotalPrice += deliveryNote.DeliveryNote.DeliveryNoteTotalPrice;
                           }%>
                            <%--FIN AGREGADO DE REMITOS--%>
                        </tbody>
                        <tfoot style="border-top: 1px solid black; font-weight: bold;">
                            <tr>
                                <td colspan="5" align="left">
                                    &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    &nbsp;
                                </td>
                                <td style="text-align: right;">
                                    <%: Index.Subtotal%>
                                </td>
                                <td>
                                    <label id="Invoice_DeliveryNotes_Total_Price">
                                        <%: DeliveryNoteTotalPrice%>
                                    </label>
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                <%--FIN REMITOS--%>
                <%--INICIO REPUESTOS--%>
                <div class="form-modulo">
                    <div class="form-modulo-title">
                        Repuestos
                    </div>
                    <div class="form-modulo-box" style="width: 300px;">
                        <input type="text" id="ReplacementName" /><input type="hidden" id="ReplacementID" /><input
                            type="button" id="AddReplacement" value="Agregar repuesto" class = "btn btn-lg btn-primary" style="float:right" />
                    </div>
                </div>
                <div style="width: 100%; height: 500px; text-align: center; vertical-align: middle;">
                    <table style="width: 100%;">
                        <thead style="border-bottom: 1px solid black; border-top: 1px solid black; background: whitesmoke">
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    &nbsp;
                                </th>
                                <th style="width: 50px">
                                    <%: Index.Quantity %>
                                </th>
                                <th>
                                    <%: Index.Name %>
                                </th>
                                <th>
                                    <%: Index.UnitPrice %>
                                </th>
                                <th>
                                    <%: Index.TotalPrice %>
                                </th>
                            </tr>
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                        </thead>
                        <tbody id="ReplacementTable">
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                            <%--AGREGAR REPUESTOS PREEXISTENTES--%>
                            <% var rowReplacementIndex = 0;
                               decimal replacementTotalPrice = 0;
                               foreach (Sita.Entities.InvoiceReplacement replacement in Model.InvoiceReplacements)
                               { %>
                            <tr id="rowReplacement_<%: rowReplacementIndex%>" class="detail">
                                <td>
                                    <input type="hidden" value="<%: replacement.ReplacementID %>" id="Replacement_ReplacementID_<%: rowReplacementIndex%>" />
                                    <input type="hidden" value="<%: replacement.Replacement.Stock %>" id="Replacement_Stock_<%: rowReplacementIndex%>" />
                                    <img src="../../Content/Images/cross.png" onclick="DeleteRow(<%: rowReplacementIndex%>, 'Replacement')" />
                                </td>
                                <td>
                                    <input type="text" value="<%:replacement.Quantity %>" id="Replacement_Quantity_<%: rowReplacementIndex%>"
                                        onchange="ModifyPrice(<%: rowReplacementIndex%>, 'Replacement')" />
                                </td>
                                <td>
                                    <%:replacement.Replacement.Name%>
                                </td>
                                <td>
                                    <input type="text" value="<%:replacement.UnitPrice %>" id="Replacement_UnitPrice_<%: rowReplacementIndex%>"
                                        onchange="UnitPrice_Change(this, <%: rowReplacementIndex%>, 'Replacement')" />
                                </td>
                                <td>
                                    <input readonly="readonly" type="text" id="Replacement_TotalPrice_<%: rowReplacementIndex%>"
                                        value="<%:replacement.TotalPrice %>" />
                                </td>
                            </tr>
                            <%rowReplacementIndex++;
                              replacementTotalPrice += replacement.TotalPrice;
                           }%>
                            <%--FIN AGREGADO DE REPUESTOS--%>
                        </tbody>
                        <tfoot style="border-top: 1px solid black; font-weight: bold;">
                            <tr>
                                <td colspan="5" align="left">
                                    &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    &nbsp;
                                </td>
                                <td style="text-align: right;">
                                    <%: Index.Subtotal%>
                                </td>
                                <td>
                                    <label id="Invoice_Replacements_Total_Price">
                                        <%: replacementTotalPrice%>
                                    </label>
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                <%--FIN REPUESTOS--%>
                <%--INICIO EXTRAS--%>
                <div class="form-modulo">
                    <div class="form-modulo-title">
                        Otros
                    </div>
                    <div class="form-modulo-box" style="width: 300px;">
                        <input type="text" id="ExtraItemName" /><input type="button" id="AddExtraItem" value="Agregar Otros" class = "btn btn-lg btn-primary" style="float:right"/>
                    </div>
                </div>
                <div style="width: 100%; height: 500px; text-align: center; vertical-align: middle;">
                    <table style="width: 100%;">
                        <thead style="border-bottom: 1px solid black; border-top: 1px solid black; background: whitesmoke">
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    &nbsp;
                                </th>
                                <th style="width: 50px">
                                    <%: Index.Quantity %>
                                </th>
                                <th>
                                    <%: Index.Name %>
                                </th>
                                <th>
                                    <%: Index.UnitPrice %>
                                </th>
                                <th>
                                    <%: Index.TotalPrice %>
                                </th>
                            </tr>
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                        </thead>
                        <tbody id="ExtraItemTable">
                            <tr>
                                <td colspan="5">
                                    &nbsp;
                                </td>
                            </tr>
                            <%--AGREGAR EXTRAS PREEXISTENTES--%>
                            <% var rowExtraItemIndex = 0;
                               decimal extraItemTotalPrice = 0;
                               foreach (Sita.Entities.InvoiceExtraItem extraItem in Model.InvoiceExtraItems)
                               { %>
                            <tr id="rowExtraItem_<%: rowExtraItemIndex%>" class="detail">
                                <td>
                                    <img src="../../Content/Images/cross.png" onclick="DeleteRow(<%: rowExtraItemIndex%>, 'ExtraItem')" />
                                </td>
                                <td>
                                    <input type="text" value="<%:extraItem.Quantity %>" id="ExtraItem_Quantity_<%: rowExtraItemIndex%>"
                                        onchange="ModifyPrice(<%: rowExtraItemIndex%>, 'ExtraItem')" />
                                </td>
                                <td>
                                    <input readonly="readonly" type="text" value="<%:extraItem.Description%>" id="ExtraItem_Description_<%: rowExtraItemIndex%>" />
                                </td>
                                <td>
                                    <input type="text" value="<%:extraItem.UnitPrice %>" id="ExtraItem_UnitPrice_<%: rowExtraItemIndex%>"
                                        onchange="UnitPrice_Change(this, <%: rowExtraItemIndex%>, 'ExtraItem')" />
                                </td>
                                <td>
                                    <input readonly="readonly" type="text" id="ExtraItem_TotalPrice_<%: rowExtraItemIndex%>"
                                        value="<%:extraItem.TotalPrice %>" />
                                </td>
                            </tr>
                            <%rowExtraItemIndex++;
                              extraItemTotalPrice += extraItem.TotalPrice;
                           }%>
                            <%--FIN AGREGADO DE EXTRAS--%>
                        </tbody>
                        <tfoot style="border-top: 1px solid black; font-weight: bold;">
                            <tr>
                                <td colspan="5" align="left">
                                    &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    &nbsp;
                                </td>
                                <td style="text-align: right;">
                                    <%: Index.Subtotal %>
                                </td>
                                <td>
                                    <label id="Invoice_ExtraItems_Total_Price">
                                        <%: extraItemTotalPrice%>
                                    </label>
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                <%--FIN EXTRAS--%>
                <% var invoiceTotalPrice = replacementTotalPrice + extraItemTotalPrice + DeliveryNoteTotalPrice;%>
                <div style="font-size: 20px; width: 300px; border: 2px solid black; background: whitesmoke;">
                    PRECIO TOTAL TOTAL:
                    <label id="Invoice_Total_Price">
                        <%: invoiceTotalPrice%>
                    </label>
                </div>
                <div class="form-modulo">
                    <div class="form-modulo-title" />
                    <div class="form-modulo-boton">
                        <input type="submit" class="btn btn-lg btn-primary" value="Guardar"  id="Save" />
                        <input type="button" class="btn btn-lg btn-secondary" value="Cancelar" id="Cancel" />
                    </div>
                </div>
                <div class="form-separador" />
                <div class="form-clear" />
            </center>
            <script type="text/javascript">
        $(function () {
            ValidateForm();
        });
        rowReplacementIndex = <%: rowReplacementIndex%>;
        rowExtraItemIndex = <%: rowExtraItemIndex%>;
        rowDeliveryNoteIndex = <%: rowDeliveryNoteIndex %>
        $("#Module_10ul").removeClass("select").addClass("current");
            </script>
            <%} %>
        </div>
    </div>
</asp:Content>
